package com.cn.config.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.cn.mapper.dao.db4", sqlSessionFactoryRef = "db4SqlSessionFactory")
public class DataSourConfig4 {
    @Bean("db4DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db4")
    public DataSource getDb4DataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("db4SqlSessionFactory")
    public SqlSessionFactory db4SqlSessionFactory(@Qualifier("db4DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper1/db4/**/*.xml"));
        bean.setTypeAliasesPackage("com.cn.bean.pojo");
        // 设置 MyBatis 的 VFS 实现类
        bean.setVfs(SpringBootVFS.class);
        return bean.getObject();
    }
    @Bean("db4SqlSessionTemplate")
    public SqlSessionTemplate db3SqlSessionTemplate(@Qualifier("db4SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
